package com.isunland.managesystem.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.ImageLoader.ImageListener;
import com.isunland.managesystem.R;
import com.isunland.managesystem.common.ApiConst;
import com.isunland.managesystem.common.RequestManager;
import com.isunland.managesystem.entity.CompanyForumAnswer;
import com.isunland.managesystem.utils.DateUtil;
import com.isunland.managesystem.utils.FileUtil;
import java.util.Date;
import java.util.List;

/**
 * 话题回复
 *
 * @author wu
 */
public class CompanyForumTopicAnswerListAdapter
    extends ArrayAdapter<CompanyForumAnswer.actualObject> {

  /**
   * ViewHolder class for layout.<br />
   * <br />
   * Auto-created on 2015-10-19 15:50:00 by Android Layout Finder
   * (http://www.buzzingandroid.com/tools/android-layout-finder)
   */
  private static class ViewHolder {
    public final RelativeLayout rootView;
    public final RelativeLayout rlForumAnswer;
    public final ImageView ivImageAnswerDetail;
    public final TextView tvNameAnswerDetail;
    public final TextView tvReplyPerson;
    public final TextView tvContentAnswerDetail;
    public final TextView tvTimeAnswerDetail;

    private ViewHolder(RelativeLayout rootView, RelativeLayout rlForumAnswer,
        ImageView ivImageAnswerDetail, TextView tvNameAnswerDetail, TextView tvReplyPerson,
        TextView tvContentAnswerDetail, TextView tvTimeAnswerDetail) {
      this.rootView = rootView;
      this.rlForumAnswer = rlForumAnswer;
      this.ivImageAnswerDetail = ivImageAnswerDetail;
      this.tvNameAnswerDetail = tvNameAnswerDetail;
      this.tvReplyPerson = tvReplyPerson;
      this.tvContentAnswerDetail = tvContentAnswerDetail;
      this.tvTimeAnswerDetail = tvTimeAnswerDetail;
    }

    public static ViewHolder create(RelativeLayout rootView) {
      RelativeLayout rlForumAnswer = (RelativeLayout) rootView.findViewById(R.id.rl_forum_answer);
      ImageView ivImageAnswerDetail =
          (ImageView) rootView.findViewById(R.id.iv_image_answer_detail);
      TextView tvNameAnswerDetail = (TextView) rootView.findViewById(R.id.tv_name_answer_detail);
      TextView tvReplyPerson = (TextView) rootView.findViewById(R.id.tv_reply_person);
      TextView tvContentAnswerDetail =
          (TextView) rootView.findViewById(R.id.tv_content_answer_detail);
      TextView tvTimeAnswerDetail = (TextView) rootView.findViewById(R.id.tv_time_answer_detail);
      return new ViewHolder(rootView, rlForumAnswer, ivImageAnswerDetail, tvNameAnswerDetail,
          tvReplyPerson, tvContentAnswerDetail, tvTimeAnswerDetail);
    }
  }

  @Override public View getView(int position, View convertView, ViewGroup parent) {
    final ViewHolder vh;
    if (convertView == null) {
      View view = mInflater.inflate(R.layout.adapter_forum_answer, parent, false);
      vh = ViewHolder.create((RelativeLayout) view);
      view.setTag(vh);
    } else {
      vh = (ViewHolder) convertView.getTag();
    }

    CompanyForumAnswer.actualObject item = getItem(position);

    vh.tvNameAnswerDetail.setText(item.getRegStaffName());

    String pid = item.getPid();

    if (pid != null) {
      vh.tvReplyPerson.setVisibility(View.VISIBLE);
      // 拼接回复的内容和人名
      for (int i = 0; i < mList.size(); i++) {
        String id = mList.get(i).getId();
        if (pid.equals(id)) {
          String name = mList.get(i).getRegStaffName();
          String content = mList.get(i).getDiscussContents();
          vh.tvReplyPerson.setText("@ " + name + "  :  " + content);
        }
      }
    } else {
      vh.tvReplyPerson.setVisibility(View.GONE);
    }

    vh.tvContentAnswerDetail.setText(item.getDiscussContents());

    // 头像
    String filePath = item.getPicture();

    if (!TextUtils.isEmpty(filePath)) {
      filePath = FileUtil.getThumbNailFilePath(filePath);
      String urlImage = ApiConst.getDownLoadUrlByGet(filePath);

      ImageListener listener = ImageLoader.getImageListener(vh.ivImageAnswerDetail, // 控件名
          R.drawable.photo, R.drawable.photo); // 默认头像
      mImageLoader.get(urlImage, listener);
    } else {
      vh.ivImageAnswerDetail.setImageResource(R.drawable.photo);
    }

    // 服务器返回的时间
    String times = item.getRegDate();
    String publishTime = DateUtil.dateToTimeInMillis(context, times, new Date());
    vh.tvTimeAnswerDetail.setText(publishTime);

    return vh.rootView;
  }

  private LayoutInflater mInflater;
  private List<CompanyForumAnswer.actualObject> mList;
  private Context context;
  private ImageLoader mImageLoader;

  // private ArrayList<String> mIds;
  // Constructors
  public CompanyForumTopicAnswerListAdapter(Context context,
      List<CompanyForumAnswer.actualObject> objects) {
    super(context, 0, objects);
    this.mInflater = LayoutInflater.from(context);
    this.mList = objects;
    this.context = context;
    mImageLoader = RequestManager.getImageLoader();
  }

  public CompanyForumTopicAnswerListAdapter(Context context,
      CompanyForumAnswer.actualObject[] objects) {
    super(context, 0, objects);
    this.mInflater = LayoutInflater.from(context);
    this.context = context;
  }

  /**
   * listview为空时显示header。只要返回false就可以了。
   * 表示listView不会空。
   *
   * @see android.widget.BaseAdapter#isEmpty()
   */
  @Override public boolean isEmpty() {

    return false;
  }
}
